import 'package:flutter/material.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import '../widgets/empty_view.dart';

import '../common/api_service.dart';
import '../common/constants.dart';
import '../model/cartoon_model.dart';
import 'cartoon_list_item.dart';

class MineCartoonListView extends StatefulWidget {
  final String uid;

  const MineCartoonListView({super.key, required this.uid});

  @override
  State<MineCartoonListView> createState() => _MineCartoonListViewState();
}

class _MineCartoonListViewState extends State<MineCartoonListView> with AutomaticKeepAliveClientMixin {
  final List<CartoonModel> _cartoonList = [];
  bool _isLoading = false;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    _isLoading = true;
    _getUserCartoonList();
  }

  void _getUserCartoonList() {
    ApiService().getUserCartoonList(widget.uid, (list) {
      setState(() {
        _isLoading = false;
        _cartoonList.clear();
        _cartoonList.addAll(list);
      });
    }, () {
      _isLoading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return _isLoading
        ? Center(
            child: LoadingAnimationWidget.progressiveDots(
              color: Constants.themeColor,
              size: 30,
            ),
          )
        : (_cartoonList.isNotEmpty
            ? ListView.builder(
                padding: EdgeInsets.zero,
                itemCount: _cartoonList.length,
                itemBuilder: (BuildContext context, int index) {
                  var cartoon = _cartoonList[index];
                  return CartoonListItem(cartoon: cartoon);
                },
              )
            : const EmptyView());
  }
}
